from flask import Blueprint, request
from utilsapp.mysql_con import db_connect
from utilsapp.mysql_con import db_operation
from utilsapp.common import parsing_permissions

sthred = Blueprint('sthred', __name__)


# 查看所有g
@sthred.route('/getlist', methods=['post'])
def get_sthred_list():
    flag = parsing_permissions(request)
    size = int(request.json.get('size'))
    page = int(request.json.get('page'))
    scope_id = 0 if not request.json.get('scope_id') else request.json.get('scope_id')
    device_name = 0 if not request.json.get('device_name') else request.json.get('device_name')
    sensor_name = 0 if not request.json.get('sensor_name') else request.json.get('sensor_name')
    eigenvalue = 0 if not request.json.get('eigenvalue') else request.json.get('eigenvalue')
    if scope_id != 0:
       
        scope_sql = f"""SELECT type FROM regionlevel where id = {scope_id} """
        type_ = db_connect(scope_sql)["data"][0]["type"]
        sql = f"""select st.*,s.device_id,s.zone_id,s.factory_id ,s.company_id,s.group_id,s.sensor_name,d.device_name,s.address,e.name,e.unit
        from sthred st ,sensor s,device d, eigenvalue e where s.address = st.sensorId and s.device_id = d.id and e.nickname=st.featureName
        and s.{type_}_id = {scope_id} and
        IF('{device_name}'='0' ,""="",d.device_name like '%{device_name}%') and
        IF('{sensor_name}'='0' ,""="",s.sensor_name like '%{sensor_name}%') and
        IF('{eigenvalue}'='0' ,""="",st.featureName = '{eigenvalue}') 
        LIMIT {max(0, (page - 1) * size)},{size}"""

        sql_1 = f"""select count(*) as total
        from sthred st ,sensor s,device d where s.address = st.sensorId and s.device_id = d.id and s.{type_}_id = {scope_id} and
        IF('{device_name}'='0' ,""="",d.device_name like '%{device_name}%') and
        IF('{sensor_name}'='0' ,""="",s.sensor_name like '%{sensor_name}%') and
        IF('{eigenvalue}'='0' ,""="",st.featureName = '{eigenvalue}') """
        data = db_connect(sql)
        total = db_connect(sql_1)["data"][0]['total']
       
       
        return {
            "code": 0,
            "data": data['data'],
            "pagination": {
                "page": page,
                "pageSize": size,
                "total": total,
            },
        }
    else:
        data_id = flag['dataId']
        if data_id == 0:
            whe = ""
        else:
            type_sql = f"""SELECT type FROM regionlevel where id = {data_id} """
            type_ = db_connect(type_sql)["data"][0]["type"]
            whe = f"""s.{type_}_id={data_id} and"""
        sql = f"""select st.*,s.device_id,s.zone_id,s.factory_id ,s.company_id,s.group_id ,s.sensor_name,d.device_name,s.address,s.address,e.name,e.unit
        from sthred st ,sensor s,device d ,eigenvalue e where s.address = st.sensorId and s.device_id = d.id and e.nickname=st.featureName and {whe}
        IF('{device_name}'='0' ,""="",d.device_name like '%{device_name}%') and
        IF('{sensor_name}'='0' ,""="",s.sensor_name like '%{sensor_name}%') and
        IF('{eigenvalue}'='0' ,""="",st.featureName = '{eigenvalue}') 
        LIMIT {max(0, (page - 1) * size)},{size}"""
        sql_1 = f"""select count(*) as total
        from sthred st ,sensor s,device d where s.address = st.sensorId and s.device_id = d.id and {whe}
        IF('{device_name}'='0' ,""="",d.device_name like '%{device_name}%') and
        IF('{sensor_name}'='0' ,""="",s.sensor_name like '%{sensor_name}%') and
        IF('{eigenvalue}'='0' ,""="",st.featureName = '{eigenvalue}') """
        total = db_connect(sql_1)["data"][0]['total']
        data = db_connect(sql)
       
       
        return {
            "code": 0,
            "data": data['data'],
            "pagination": {
                "page": page,
                "pageSize": size,
                "total": total,
            },
        }


@sthred.route('/edit', methods=["post"])
def change_sthred_maxThred():
   
    factoryId = request.json.get('factoryId')
    sensorId = request.json.get('sensorId')
    featureName = request.json.get('featureName')
    maxThred = request.json.get('maxThred')
   
    sql_1 = f"""update sthred set maxThred = {maxThred} where sensorId = '{sensorId}' and featureName='{featureName}' and factoryId='{factoryId}'"""
    res = db_operation(sql_1)
   
   
    affected_rows = res['Affected_rows']
    if affected_rows >= 1:
        return {
            'code': 0,
            'msg': '修改成功',
            'data': res
        }
    else:
        return {
            'code': -1,
            'msg': '修改失败'
        }
